require(plyr)
require(reshape)
require(ggplot2)
require(directlabels)
require(gridExtra)


media <- read.csv("MASSMEDIA_Data.csv", header=TRUE, stringsAsFactors=FALSE) # Not provided

tv_tot <- subset(media, Tipo.dato=="persone di 3 anni e piu che guardano la tv" &
                   Sesso=="totale" & Classe.di.eta=="totale", select=c("Anno","Value"))
paper_tot <- subset(media, Tipo.dato=="persone di 6 anni e piu che leggono quotidiani almeno una volta a settimana" &
                   Sesso=="totale" & Classe.di.eta=="totale", select=c("Anno","Value"))
internet <- read.csv("/Users/francesco/Dropbox/Thesis_PhD/data/internet_use_istat/USOINTPC_Data.csv", header=TRUE, stringsAsFactors=FALSE)

internet_tot <- aggregate(Value ~ Anno, subset(internet,
                       (Uso.e.frequenza=="si, una o piu volte a settimana ma non tutti i giorni" |
                          Uso.e.frequenza=="si, tutti i giorni") &
                       Sesso=="totale" & Classe.di.eta=="totale" &
                       Tipo.dato=="persone  di 6 anni e piu che hanno usato Internet negli ultimi 12 mesi",
                       select=c("Anno","Value")), FUN=sum)

media_use_df <- merge(merge(tv_tot, paper_tot, by.x="Anno", by.y="Anno", all=TRUE), internet_tot, by.x="Anno", by.y="Anno", all=TRUE)
media_use_df <- rename(media_use_df, c("Value.x"="TV","Value.y"="Newspaper",
                                       "Value"="Internet"))

media_use_df <- melt(media_use_df, id="Anno")

plot1 <- ggplot(media_use_df, aes(x=Anno, y=value)) + 
  geom_line(aes(group=variable, colour=variable)) + theme(legend.position="none") +
  geom_dl(aes(label=variable),method="smart.grid") + labs(x=NULL, y="Of total population (%)") +
  theme(axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        plot.margin = unit(c(0.5,0.5,0,0.5), "cm")) +
  expand_limits(y = 103)

# Difference over previous year
media_use_df_diff <- merge(merge(data.frame(Anno=tv_tot$Anno[2:nrow(tv_tot)],
                                            TV=diff(tv_tot$Value)),
                                data.frame(Anno=paper_tot$Anno[2:nrow(paper_tot)],
                                            Newpaper=diff(paper_tot$Value)),
                                by="Anno"),
                           data.frame(Anno=internet_tot$Anno[2:nrow(internet_tot)],
                                      Internet=diff(internet_tot$Value)),
                           by="Anno", all.x=TRUE)

# Add missing year
media_use_df_diff <- rbind(media_use_df_diff, c(1993,NA,NA,NA))

media_use_df_diff <- melt(media_use_df_diff, id="Anno")
plot2 <- ggplot(media_use_df_diff, aes(x=Anno, y=value)) + 
  geom_hline(yintercept=0, size=1, alpha=0.4) +
  expand_limits(y = 6) + geom_line(aes(group=variable, colour=variable)) + theme(legend.position="none") +
  geom_dl(aes(label=variable),method="top.bumpup") + labs(x=NULL, y="Difference over previous year (%)") +
  theme(plot.margin = unit(c(0,0.5,0.5,0.5), "cm"))

gp1<- ggplot_gtable(ggplot_build(plot1))
gp2<- ggplot_gtable(ggplot_build(plot2))
require(grid)
maxWidth = unit.pmax(gp1$widths[2:3], gp2$widths[2:3])
gp1$widths[2:3] <- maxWidth
gp2$widths[2:3] <- maxWidth

grid.arrange(gp1,gp2)

